package com.whately.core.base.constant;

import com.whately.core.base.model.RedisKeyBuilder;
import org.apache.commons.lang3.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * 描述：reids 缓存 key  定义
 *
 * @author craig.qin
 */
public enum WhatelyCoreCacheKeyEnum {

	/****************************************
	 *   core 配置信息
	 ****************************************/
	CORE_CONFIG_LOG_ASPECT_OPEN("core:myconfig:whlog:open","WhLogAllAspect日志配置", 30, TimeUnit.MINUTES),
	CORE_CONFIG_LOG_ASPECT_PRINT_PARAM("core:myconfig:whlog:openPrintParam","WhLogAllAspect日志配置", 30, TimeUnit.MINUTES),
	CORE_CONFIG_RBAC_SUPER_ADMIN("core:myconfig:rbac:superadmin:enable","超级用户配置", 30, TimeUnit.MINUTES),
	/****************************************
	 *   token 验证码缓存
	 ****************************************/
	TOKEN_VERIFY_CODE("core:token:verifyCode:","token图片验证码", 3, TimeUnit.MINUTES),
	TOKEN_VERIFY_CODE_USED("core:token:verifyCodeUsed:","使用过的token图片验证码", 4, TimeUnit.MINUTES),
	TOKEN_VERIFY_CODE_SMS("core:token:verifyCodeSMS:","token短信验证码", 3, TimeUnit.MINUTES),

	/****************************************
	 *  手机验证码发送规则
	 ****************************************/
	SMS_CODE_SEND_RULE_1m("core:sms:send:1m:","手机验证码发送规则", 1, TimeUnit.MINUTES),
	SMS_CODE_SEND_RULE_24h("core:sms:send:24h:","手机验证码发送规则", 24, TimeUnit.HOURS),


	/****************************************
	 *  风控规则
	 ****************************************/
	RISK_CTRL_CHECK_IP("core:risk:check:ip:","登录风控验证IP", 2, TimeUnit.MINUTES),
	RISK_CTRL_CHECK_ID("core:risk:check:id:","登录风控验证ID", 2, TimeUnit.MINUTES),
	RISK_CTRL_RETRY_ID("core:risk:retry:id:","ID尝试的次数", 1, TimeUnit.MINUTES),

	/****************************************
	 *  id自增规则
	 ****************************************/
	CORE_INCREASE_ID_SECOND("core:increase:id:second:","自增主键", 5, TimeUnit.MINUTES),


	;

	// 缓存的key
	private String cacheKey;

	private String desc;

	// 默认的过期时间
	private int defaultExpireTime;

	// 缓存时间单位
	private TimeUnit timeUnit;


	WhatelyCoreCacheKeyEnum(String cacheKey, String desc, int defaultExpireTime, TimeUnit timeUnit) {
		this.cacheKey = cacheKey;
		this.desc = desc;
		this.defaultExpireTime = defaultExpireTime;
		this.timeUnit = timeUnit;
	}

	/**
	 * 获取该枚举的信息
	 *
	 * @param key
	 * @return
	 */
	public static WhatelyCoreCacheKeyEnum getEnumByKey(String key) {
		if (StringUtils.isEmpty(key)) {
			return null;
		}
		for (WhatelyCoreCacheKeyEnum e : values()) {
			if (key.equalsIgnoreCase(e.name())) {
				return e;
			}
		}
		return null;
	}

	public String getCacheKey() {
		return cacheKey;
	}

	public int getDefaultExpireTime() {
		return defaultExpireTime;
	}

	public TimeUnit getTimeUnit() {
		return timeUnit;
	}

	public String getDesc() {
		return desc;
	}


	public RedisKeyBuilder obtainKeyBuilder(){
		RedisKeyBuilder rkb = RedisKeyBuilder.newInstance();
		rkb.appendFixed(cacheKey);
		rkb.setDefaultExpireTime(defaultExpireTime);
		rkb.setTimeUnit(timeUnit);
		return rkb;
	}
}
